package com.ll.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import com.ll.common.MessageConstant;
import com.ll.pojo.DTO.DeleteRequest;
import com.ll.pojo.DTO.SearchUserDTO;
import com.ll.pojo.DTO.UserDTO;
import com.ll.pojo.DTO.UserLoginDTO;
import com.ll.pojo.VO.PageVO;
import com.ll.pojo.entity.User;
import com.ll.service.UserService;
import com.ll.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PageHelperProperties pageHelperProperties;

    @RequestMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO,HttpSession session) {
        User user = userService.login(userLoginDTO);
        if (user == null){
            log.info("用户不存在");
            return Result.error(MessageConstant.USER_NOT_FOUND);
        }
        log.info("用户登录成功=>", user);
        session.setAttribute("user",user);
        return Result.success(user);
    }

    @GetMapping("/info")
    public Result getUserInfo(SearchUserDTO searchUserDTO) {
        PageHelper.startPage(searchUserDTO.getPageNum(),searchUserDTO.getPageSize());
        List<User> userInfo = userService.getUserInfo(searchUserDTO);
        if (userInfo == null){
            log.info("获取用户信息失败");
            return Result.error(MessageConstant.GET_USER_FAIL);
        }
        PageVO pageVO = new PageVO();
        PageInfo<User> pageInfo = new PageInfo<>(userInfo);
        pageVO.setPageNum(pageInfo.getPageNum());
        pageVO.setPageSize(pageInfo.getPageSize());
        pageVO.setTotal((int) pageInfo.getTotal());
        pageVO.setPages(pageInfo.getPages());
        pageVO.setList(pageInfo.getList());
        return Result.success(pageVO);
    }

    @DeleteMapping("/delete")
    public Result deleteUser(@RequestBody DeleteRequest deleteRequest) {
        List<Long> userIds = deleteRequest.getIds();
        if (userIds == null || userIds.isEmpty()) {
            return Result.error("请选择要删除的用户ID");
        }
        int delete = userService.deleteUser(userIds);
        if (delete < 1) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    @PostMapping("/add")
    public Result addUser(@RequestBody UserDTO  userDTO) {
        int add = userService.addUser(userDTO);
        if (add < 1) {
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        int update = userService.updateUser(userDTO);
        if (update < 1) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

    @GetMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("user");
        return Result.success("退出成功");
    }
}
